package com.JiaLiang.ssm.controller;

import com.JiaLiang.ssm.pojo.User;
import com.JiaLiang.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;

@Controller
public class UserController {

    @Autowired
    private  UserService userService;

    //表单提交过来的路径
    @RequestMapping("checkLogin")
    public String Login(String password,String username, Model model){
        //调用service方法
        User user = userService.Login(username,password);
        //若有user则添加到model里并且跳转到成功页面
        if(user != null &&user.getPassword().equals(password)&&user.getUsername().equals(username)){
            model.addAttribute("user",user);
            System.out.println(model);
            return "main";
        }
        return "fail";
    }

    @RequestMapping("checkRegister")
    public String Register(User user,Model model){
        if(userService.usernameExist(user.getUsername())){
            return "fail";
        }
        userService.Register(user);
        model.addAttribute("user",user);
        return "main";
    }

}
